//
//  TimeExtension.swift
//  ZJTools
//
//  Created by 万孟超 on 2023/10/26.
//

import Foundation

extension Double {
    var toDateString : String {
        let date = Date.init(timeIntervalSince1970: self)
        let dateformat = DateFormatter()
        dateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dateformat.string(from: date)
    }
}

extension Int {
    var hexString: String {
        let str = String(format: "%0X", self)
        if str.count == 1 {
            return "0" + str
        }else{
            return str
        }
    }
}

extension String {
    /// String转换成时间戳
    var toTimeIntervalString : String {
        let dateformat = DateFormatter()
        dateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let timeDate = dateformat.date(from: self)
        return Self(format: "%.0f", timeDate?.timeIntervalSince1970 ?? 0)
    }
    
    /// 转换为Int ，转换不成功返回最小的Int值
    var toDouble : Double {
        return (self as NSString).doubleValue
    }
    
    /// 转换为Int ，转换不成功返回最小的Int值
    var toInt : Int {
        return (self as NSString).integerValue
    }
    
    /// 时间戳转换为时间
    var toDateString : String {
        return toDouble.toDateString
    }
    
    var isTimestamp:  Bool {
       return toDouble != 0
    }
    
    var hexToTen: (String, String, String) {
        //16进制字符串转换10进制
        let text = Scanner.init(string: self)
        var colorValue:UInt64 = 0
        text.scanHexInt64(&colorValue)
        let red = CGFloat((colorValue & 0xFF0000)>>16)/0xFF
        let green = CGFloat((colorValue & 0x00FF00)>>8)/0xFF
        let blue = CGFloat((colorValue & 0x0000FF))/0xFF
        return (Self(format: "%g", red * 255.0), Self(format: "%g", green * 255.0), Self(format: "%g", blue * 255.0))
    }
}
